package com.droid.persistor.jdbc;

import java.util.*;
import java.io.*;
import java.sql.*;

import org.apache.log4j.Logger;
/**Clase que maneja la conexion a la base de datos, de ella extienden todas las clases dao
 * */
public class DatabaseConnection{


//private static java.util.ResourceBundle resconf = java.util.ResourceBundle.getBundle("conf");
/*static  String className= "sun.jdbc.odbc.JdbcOdbcDriver";
static String connStr = "jdbc:odbc:baseTp";
static String user ="";	
static String passwd = "";*/
	static  String className= "oracle.jdbc.driver.OracleDriver";
	static String user ="droid";	
	static String passwd = "XpDr0id";
	//static String connStr = "jdbc:oracle:thin:@xpdroid.no-ip.biz:1524:XE";
	static String connStr = "jdbc:oracle:thin:@192.168.1.105:1524:XE";
	
	
private Connection caclsDatabaseConnection;
static final Logger logger = Logger.getLogger(DatabaseConnection.class);
//static String className = "sun.jdbc.odbc.JdbcOdbcDriver";
//static String connStr = "jdbc:odbc:Driver={Microsoft Acces Driver (* mdb )}; DBQ=c:/tmp/base,Dreiver ID=22";
//static String user = "";
//static String passwd = "";

protected ResultSet rs;
private Statement stmt;
private Statement pStmt;


	public DatabaseConnection(){
	
	}


	static{	try {
		Class.forName(className);
	}
	catch(ClassNotFoundException e) {
		e.printStackTrace();
	 	System.out.println("Oracle Driver Class not found");
	}
	}
	
	/**Metodo que obtiene la conexion a la base de datos
	 * @return Devuelve un Connection
	 * */
	protected Connection getConnection(){
		
		try{
		if (caclsDatabaseConnection ==null ||caclsDatabaseConnection.isClosed()){
			//caclsDatabaseConnection= DriverManager.getConnection(connStr+ ',' + user + ',' + passwd);
			caclsDatabaseConnection= DriverManager.getConnection(
					"jdbc:oracle:thin:@192.168.1.105:1524:XE", "droid",
					"XpDr0id");
		}
		
		}
		catch(Exception e)
		{
			logger.info("Error in connection");
			System.out.println("Error in connection");
		}
		try{
		caclsDatabaseConnection.setAutoCommit(true);
		}
		catch (SQLException e){
		
		}
		return caclsDatabaseConnection;
		}
		
	/**
	 * @return devuelve el ResultSet de la query, para que la clase dao lo ponga en memoria 
	 * */
	protected ResultSet getResultSet(Connection con,String query)throws SQLException{
	  if(con==null ||con.isClosed()){
	    con=getConnection();
	  }
	  stmt=con.createStatement();
	  rs=stmt.executeQuery(query);
	  
	return rs;
	}/* end of get ResultSet */


	protected void execute(Connection con,String query)throws SQLException{
	  if(con==null ||con.isClosed()){
	    con=getConnection();
	  }
	  stmt=con.createStatement();
	  stmt.executeUpdate(query);
	  
	}
	  
	
	protected void closeStatement(){
	  try{
	    if (rs != null){
	      rs.close();
	    }
	    if (stmt != null){
	      stmt.close();
	    }
	  }
	  catch (SQLException e){
	    e.printStackTrace();
	    logger.error("Error in closeStatement");
	  }
	}
	protected void closeStatement(ResultSet rs){
	  try{
	    if (rs != null){
	     // stmt = rs.getStatement();
	      rs.close();
	    }
	    if (stmt != null){
	      stmt.close();
	    }
	
	  }
	  catch (SQLException e){
	    e.printStackTrace();
	    System.out.println("Error in closeStatement");
	  }
	}
}/* end of class */